package com.lantosec.hmrp.modules.sysdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.sysdata.entity.SysModelForm;
import com.lantosec.hmrp.modules.sysdata.mapper.SysModelFormMapper;
import com.lantosec.hmrp.modules.sysdata.service.ISysModelFormService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 流程与表单的关系表 服务
 *
 * @author lichaowei
 * @date 2020/4/27
 */
@Service
@Primary
public class SysModelFormServiceImpl extends ServiceImpl<SysModelFormMapper, SysModelForm> implements ISysModelFormService {

    @Override
    public boolean isExist(String modelId, String tableId) {
        int count = baseMapper.selectCount(new QueryWrapper<SysModelForm>()
                .eq(String.valueOf(SysModelForm.ColName.model_id), modelId)
                .eq(String.valueOf(SysModelForm.ColName.form_table_id), tableId));
        return count > 0;
    }

    @Override
    public SysModelForm selectByModelId(String modelId) {
        return baseMapper.selectOne(new QueryWrapper<SysModelForm>()
                .eq(String.valueOf(SysModelForm.ColName.model_id), modelId));
    }

    @Override
    public int deleteByModelId(String modelId) {
        return baseMapper.deleteByModelId(modelId);
    }

    @Override
    public boolean save(String modelId, String tableId, String layoutId, String startTaskId, String category, String modelName, String processNameRule) {
        boolean flag = false;
        tableId = StringUtils.isBlank(tableId) ? "" : tableId;
        category = StringUtils.isBlank(category) ? "" : category;
        modelName = StringUtils.isBlank(modelName) ? "" : modelName;
        SysModelForm dbSysProForm = selectByModelId(modelId);
        if (!Objects.isNull(dbSysProForm)) {
            dbSysProForm.setProcessName(modelName);
            dbSysProForm.setProcessType(category);
            dbSysProForm.setModelId(modelId);
            dbSysProForm.setFormTableId(tableId);
            dbSysProForm.setStartLayoutId(layoutId);
            dbSysProForm.setStartTaskId(startTaskId);
            dbSysProForm.setEditTime(new Date());
            dbSysProForm.setProInsNameRule(processNameRule);
            flag = super.update(dbSysProForm, new UpdateWrapper<SysModelForm>()
                    .eq(String.valueOf(SysModelForm.ColName.model_id), modelId));
        } else {
            SysModelForm modelForm = new SysModelForm();
            String uuid = UUIDUtils.get();
            modelForm.setModelFormId(uuid);
            modelForm.setProcessName(modelName);
            modelForm.setProcessType(category);
            modelForm.setModelId(modelId);
            modelForm.setFormTableId(tableId);
            modelForm.setStartLayoutId(layoutId);
            modelForm.setStartTaskId(startTaskId);
            modelForm.setCreateTime(new Date());
            modelForm.setProInsNameRule(processNameRule);
            flag = super.save(modelForm);
        }
        return flag;
    }
}
